﻿
@using ZDSoft.UPMS.Model;
@using ZDSoft.UPMS.Model.SystemPart;
@model ZDSoft.UPMS.Model.BusinessPart.Organization
@{
    Layout = null;
    IList<User> teachers = TempData["teachers"] as IList<User>;
    <div style="display: none;" id="teacherInifo">@ViewBag.teacher</div>
}
<!DOCTYPE html>
<html>
<head>
    <title>Peizhi</title>
    <script src='@Url.Content("../../Scripts/jquery-1.5.1.min.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Content/css/SimpleDialog.css")' rel="stylesheet" type="text/css" />
    <script src='@Url.Content("~/Scripts/jquery.simplemodal.1.4.1.min.js")' type="text/javascript"></script>
    <link href='@Url.Content("../../Scripts/PageCreater/Css/All.css")' rel="stylesheet" type="text/css" />
    @*    <link href="../../Scripts/zTree/css/zTreeStyle/orgain.css" rel="stylesheet" type="text/css" />*@
    @*        <link href="../../Scripts/PageCreater/Css/Style.css" rel="stylesheet" type="text/css" />*@
    <script language="javascript" type="text/javascript">
        function SubmitPage() {
            var selectedCourseId = "";
            $("input:checkbox[checked='true']", "#Roletable").each(function (i) {
                var id = $(this).attr("id");
                id = id.replace("chk_", "");
                selectedCourseId = selectedCourseId + "," + id;
            });

            $("#selectCoursID").val(selectedCourseId);
            SaveCours();
            $("#teachers").hide();

        }



        var tr = null;
        function SaveCours() {
            var selectedCourseId = $("#selectCoursID").val();
            var OragainzationID = $("#OragainzationID").val();
            if (selectedCourseId == "") {
                alert("未选择课程");
            }
            else {
                $.ajax({
                    url: "/Organization/CoursCreate", //指定要调用的方法
                    type: "POST", //指定提交表单的方法
                    data: { "selectedCourseId": selectedCourseId, "id": OragainzationID }, //指定要提交的数据
                    dataType: "text", //指定返回数据的格式
                    beforeSend: function () { $.modal("<img src='@Url.Content("../../Content/images/loading.gif")' />"); },
                    success: function () {
                        alert("保存成功！");
                        window.location = OragainzationID;
                    },
                    Error: function () {
                        alert("保存失败！");
                    },
                    complete: function () {
                        $.modal.close();
                    }

                });
            }
        }
        function SaveTeachers() {
            var selectedCourseId = tempT;
            var SelectedTeacherId = $("#SelectedTeacherId").val();
            if (SelectedTeacherId == "") {
                alert("未选择教师");
            }
            else {
                var TechertechingId = $("#OragainzationID").val();
                $.ajax({
                    url: "/Organization/Creates", //指定要调用的方法
                    type: "POST", //指定提交表单的方法
                    data: { "SelectedTeacherId": SelectedTeacherId, "id": TechertechingId, "selectedCourseId": selectedCourseId }, //指定要提交的数据

                    dataType: "text", //指定返回数据的格式

                    success: function (data) {
                        var teacherInfos = data.split(',');
                        var td = "";

                        for (var i = 0; i < teacherInfos.length; i++) {
                            td += teacherInfos[i] + ",";

                        }
                        td = td.substring(0, td.length - 1)
                        $("td:eq(3)", tr).text(td);

                        alert("创建成功！");

                    },
                    Error: function (data) {
                        alert(data);
                        alert("创建失败！");
                    }

                });
                $("#teachers").hide();
            }

        }

        function SubmitPageTeacher() {
            var selectedCourseId = "";
            var temp = 0
            $("input:checkbox[checked='true']", "#teacherss").each(function (i) {
                var id = $(this).attr("id");
                id = id.replace("chk_", "");
                selectedCourseId = selectedCourseId + "," + id;

            });
            $("#SelectedTeacherId").val(selectedCourseId);
            SaveTeachers();

        }

        //清空当前数据
        function teachers(t) {
            tempT = t;
            tmp = 0;
            $("#teachers").show();
            $("input:checkbox", "#teacherss").each(function (i) {
                var id = $(this).attr("id");
                document.getElementById(id).checked = false;
            });
        }
        var tmp = 0;

        $(document).ready(function () {
            var a = "";
            var teacherInfo = $("#teacherInifo").text();
            var info = teacherInfo.split("|");
            for (var i = 0; i < info.length - 1; i++) {
                var tea = info[i].split(",");
                var texts = $("#" + tea[0]).html().trim();
              
                 if (texts == '未配置') {
                    $("#" + tea[0]).text(tea[1]);
                } else {
                    a = $("#" + tea[0]).html();
                    a += "," + tea[1];
                    $("#" + tea[0]).html(a);
                }
            }
             

            $("#teacherss :checkbox").each(function () {
                $(this).change(function () {
                    if ($(this).attr("checked") == true) {
                        tmp++;
                    }
                    else {
                        tmp--;
                    }
                    if (tmp >= 3) {
                        $(this).removeAttr("checked");
                        alert("最多只能配置两个教师");
                    }
                })
            });
        })

        function Check(id, obj) {
            tr = $(obj).parent().parent();
            tempT = id;
            tmp = 0;
            $("#teachers").show();
            $("input:checkbox", "#teacherss").each(function (i) {
                var id = $(this).attr("id");
                document.getElementById(id).checked = false;
            });
            $("#chk_" + id).attr("checked", "cheacked");
        }
        function Hide() {
            $("#teachers").hide();
        }
    </script>
</head>
<body>
    <form id="form" method="post" action="ConfigurationOrgainazation">
    <div style="width: 300px; float: left;">
        <fieldset style="width: 400px;">
            <legend>课程</legend>
            @if (Model.Organizations.Count == 0 || Model.Organizations == null)
            {
                <label style="color: Blue; font-size: 12px;">
                    此为配置课程和配置教师，请先配置课程，否则将会重置配置教师<br />
                    的信息</label>
            }
            else
            {
                <label style="color: Blue; font-size: 12px;">
                    此为批量配置课程，如果该教研室的班级已经配置课程则无效</label>
            }
            <table id="Roletable" style="width: 90%; font-size: 14px; border: 1px solid #007ea2;
                text-align: center;">
                <tr class="blueTableTitle">
                    <td>
                        选择
                    </td>
                    <td>
                        课程名称
                    </td>
                    @if (Model.Organizations == null || Model.Organizations.Count == 0)
                    {
                        <td>
                            教师
                        </td>
                        <td>
                            已配置教师
                        </td>
                    }
                </tr>
                @foreach (var item in (IList<ZDSoft.UPMS.Model.BusinessPart.Course>)TempData["List"])
                {
                    var hasRole = false;
                    if (Model.TeacherTeachingCourses.Where(o => o.Course.ID == item.ID).Count() > 0)
                    {
                        hasRole = true;
                    }
                    <tr>
                        <td>
                            @{var id = "chk_" + @item.ID;
                            }
                            @if (hasRole)
                            {
                                <input type="checkbox" id='@id'  checked="checked"/>
                            }
                            else
                            {
                                <input type="checkbox" id='@id'  />
                            }
                        </td>
                        <td>
                            @item.Name
                        </td>
                        @if (Model.Organizations.Count == 0 || Model.Organizations == null)
                        {
                            <td id="peizhi" style="cursor: pointer;">
                                <span onclick="Check('@item.ID',this)" style=" color:#007ea2; ">配置教师</span>
                            </td>
                            <td id="@item.ID" >
                               未配置
                            </td>
                          
                             
                        }
                    </tr>
           
                }
            </table>
            <input id="selectCoursID" name="selectCoursID" type="hidden" />
            <input id="OragainzationID" name="OragainzationID" value="@Model.ID"  type="hidden"/>
            <input type="button" onclick="SubmitPage()" class="btn3" value="保存" />
        </fieldset>
    </div>
    <div id="teachers" style="display: none; width: 300px; float:right; padding-right:19%;">
        <fieldset style="width: 300px;" id="teacher">
            <legend>任课教师</legend>
            <table style="width: 90%; font-size: 14px; border: 1px solid #007ea2; text-align: center;"
                id="teacherss">
                @if (teachers == null || teachers.Count == 0)
                {
                    <tr class="blueTableTitle">
                        <td>
                            当前机构下没有可用的教师.
                        </td>
                    </tr>
                }
                else
                {
                    <tr class="blueTableTitle">
                        <td>
                            选择
                        </td>
                        <td>
                            教师名称
                        </td>
                    </tr>
                    foreach (var item in teachers)
                    {<tr>
                        <td>
                            <input type="checkbox" name="cbname"   id='chk_ @item.ID'   />
                        </td>
                        <td>
                            @item.Name
                        </td>
                    </tr>
                    }
                }
            </table>
            <input type="text" style="display: none;" id="SelectedTeacherId" name="SelectedTeacherId" />
            <input type="button" onclick="SubmitPageTeacher()" class="btn3" value="保存" />
            <input type="button" onclick="Hide()" class="btn3" value="关闭" />
        </fieldset>
    </div>
    @* </div>*@
    </form>
</body>
</html>
